package com.suxin.socket;

import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Tang
 * @classname SocketServer_1
 * @description [  ]
 * @date 2021/4/9 20:53
 */
public class SocketServer_1 {

    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(9999, 20);
        while (true) {
            System.out.println("等待客户端连接...");
            Socket socket = serverSocket.accept();
            System.out.println("进来一个客户端连接...ip:" + socket.getInetAddress() + ",port:" +socket.getPort());
            new Thread(() -> {
                try (InputStream inputStream = socket.getInputStream()) {
                    while (true) {
                        byte[] bytes = new byte[1024];
                        int read = inputStream.read(bytes);
                        System.out.println("字节数：" + read + " 接收到的内容为：" + new String(bytes,0,read));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

}